
#include "sqlite.h"

#include "glog/logging.h"

using namespace SQL;

std::ostream& operator<<( std::ostream& stream, const Localize& localize ) {
  stream << "id: " << localize.id
    << "\n" << "state: " << localize.state
    << "\n" << "state: " << localize.state
    << "\n" << "featfile: " << localize.featfile
    << "\n" << "descfile: " << localize.descfile
    << "\n" << "imgwidth: " << localize.imgwidth
    << "\n" << "imgheight: " << localize.imgheight
    << "\n" << "localizeokay: " << localize.localizeokay
    << "\n" << "pos: " << localize.posx << " , " << localize.posy << " , " << localize.posz
    << "\n" << "quat: " << localize.quatw << " , " << localize.quatx << " , " << localize.quaty << " , " << localize.quatz;
    return stream;
}


bool SqliteHandler::open() {
  const std::string db_file{ db_root + db_filename };
  if ( !sqlite3_open( db_file.c_str(), &db ) )
  {
    VLOG( 2 ) << "open sqlite db file okay: " + db_file;
    return true;
  }
  LOG( ERROR ) << "open sqlite db file fail: " + db_file << '\n' << sqlite3_errmsg(db);
  return false;
}

// find possible query picture and get the id value
bool SqliteHandler::FindQueryPicture( Localize& localize_sql_model )
{
  localize_sql_model = Localize();
  std::string sql{ "SELECT * FROM Localize WHERE state = 1;" };
  char* cErrMsg;
  if ( SQLITE_OK != sqlite3_exec( db, sql.c_str(), FindCallback, (void*)(&localize_sql_model), &cErrMsg ) ) {
    LOG( ERROR ) << "sql find error: " << cErrMsg;
    return false;
  }
  // sql = "UPDATE Localize set state = 2 WHERE id = " + std::to_string( localize_sql_model.id);
  // VLOG( 2 ) << "localize_sql_model.state == 1 : " << (localize_sql_model.state);
  return ( localize_sql_model.state == 1 );
}


// write localize result into table
bool SqliteHandler::WriteLocalizeResult( const Localize& sql_model )
{
  /* Create merged SQL statement */
  std::ostringstream stringStream;
  stringStream << "UPDATE Localize set state = " << 3
    << " , localizeokay = " << sql_model.localizeokay
    << " , time = " << sql_model.time
    << " , posx = " << sql_model.posx
    << " , posy = " << sql_model.posy
    << " , posz = " << sql_model.posz
    << " , quatw = " << sql_model.quatw
    << " , quatx = " << sql_model.quatx
    << " , quaty = " << sql_model.quaty
    << " , quatz = " << sql_model.quatz
    << " WHERE id = " << sql_model.id;

  std::string sql = stringStream.str();
  VLOG( 2 ) << " write sql: " << sql;

  char* cErrMsg;
  if ( SQLITE_OK != sqlite3_exec( db, sql.c_str(), 0, 0, &cErrMsg ) ) {
    LOG( ERROR ) << "sql write error: " << cErrMsg;
    return false;
  }
  return true;
  return true;
}

// FindQueryPicture sql select call back function
int SqliteHandler::FindCallback( void *data, int argc, char **argv, char **azColName )
{
 /* for (int i = 0; i < argc; i++ ) {
    VLOG(2) << azColName[i] << " = " << argv[i];
  }
  VLOG( 2 ) << "----------";*/

  Localize* localize = (Localize*)data;
  
  localize->id = atoi( argv[0] );
  localize->state = atoi( argv[1] );
  localize->featfile = std::string( argv[2] );
  localize->descfile = std::string( argv[3] );
  localize->imgwidth = atoi( argv[4] );
  localize->imgheight = atoi( argv[5] );
  // VLOG( 2 ) << *localize;

  return 0;
}